<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/28
 * Time: 13:52
 */
namespace common\sources\read\taskorder;

use common\models\craftsman\BuyUser;
use common\models\craftsman\TaskOrder;
use yii\data\Pagination;

class TaskOrderAccess implements ITaskOrderAccess
{
    public function getTaskOrderById($task_order_id)
    {
        return TaskOrder::find()
                ->select("id,plat_type,task_type,task_commod_id,bus_id,buy_id,tbname,main_url1,main_url2,title,price,num,size,ship_addr,shop_name,shop_id,port_type,task_id,order_status,advance_amount,refund_amount,commission,receive_name,receive_phone,
                pro_name,pro_code,city_name,city_code,area_name,area_code,addr,return_type,task_order_num,browse_time,order_time,merchant_time,favorable_time,plat_return_time,confirm_time,reject_time,give_up_time,search_time,bus_pay_url,serial_num,
                ww_name,range_price,is_check,task_shop_status,addr_url,pay_url,parise_url,tao_parise_url,keyword_url,pay_order_num,qr_code_url,keywords,task_shop_image_id,task_commod_keyword_id,praise_keywords_id")
                ->where(['id'=> $task_order_id])
                ->asArray()
                ->one();
    }
    public function PayOrderList($data,$count = false)
    {
        $query = TaskOrder::find()
            ->alias('t')
            ->select("t.price,t.num,pay_order_num,t.id,task_order_num,t.create_time,buy_id,order_time,actual_price,t.pro_name,t.pro_code,t.city_name,t.city_code,t.area_name,t.area_code,
            t.addr,t.bus_id,purchase_num,addr_url,pay_url,tbname,t.ww_name,t.task_commod_id,t.task_id,
            phone,qq,shop.shop_name,receive_name,receive_phone")
            ->leftJoin("buyuser","t.buy_id = buyuser.id")
	        ->leftJoin('task_commod','task_commod.id = t.task_commod_id')
	        ->leftJoin("shop","task_commod.shop_id = shop.id")
	        ->where(['order_status' => 2]);
        if (!empty($data['plat_type'])) {
            $query = $query->andWhere(['plat_type' => $data['plat_type']]);
        }
        if (1 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['task_commod_id' => $data['value']]);
        }
        if (2 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['task_order_num' => $data['value']]);
        }
        if (3 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['pay_order_num' => $data['value']]);
        }
        if (4 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['tbname' => $data['value']]);
        }
        if (5 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['phone' => $data['value']]);
        }
        if (6 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['phone' => $data['value']]);
        }
        if (7 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['shop.shop_name' => $data['value']]);
        }
        if (8 == $data['search_type'] && !empty($data['value'])) {
            $query = $query->andWhere(['shop.ww_name' => $data['value']]);
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'order_time',
                $data['begin_time'],
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'order_time',
                $data['end_time'],
            ]);
        }
        if ($count) {
            $result = $query
                ->asArray()
                ->count();
            return $result;
        }
        $result = $query
            ->offset(($data['page_num']-1)*$data['limit'])
            ->limit($data['limit'])
            ->asArray()
            ->all();
        return $result;
    }
    //统计总数量
    public function getOrderListCount($data)
    {
        $where = $this->getOrderListWhere($data);
        $models = TaskOrder::find()
                ->alias("t");
        if($data['search_type'] == 6 && !empty($data['search_value']))
        {
            $models->leftJoin("buyuser bu","bu.id = t.bus_id");
        }else if($data['search_type'] == 7 && !empty($data['search_value']))
        {
            $models->leftJoin("buyuser u","u.id = t.buy_id");
        }else if($data['search_type'] == 8 && !empty($data['search_value']))
        {
            $models->leftJoin("taobao tb","tb.id = t.tb_id");
        }

        if($data['search_type2'] == 6 && !empty($data['search_value2']))
        {
            $models->leftJoin("buyuser bu","bu.id = t.bus_id");
        }else if($data['search_type2'] == 7 && !empty($data['search_value2']))
        {
            $models->leftJoin("buyuser u","u.id = t.buy_id");
        }else if($data['search_type2'] == 8 && !empty($data['search_value2']))
        {
            $models->leftJoin("taobao tb","tb.id = t.tb_id");
        }

        return $models->where($where)->count();
    }
    //获取列表数据
    public function getOrderLists($data)
    {
        $where = $this->getOrderListWhere($data);
        $query = TaskOrder::find()
            ->alias('t')
            ->select("t.return_type,t.task_type,t.id,task_order_num,t.create_time,t.buy_id,t.order_time,t.actual_price,t.pro_name,t.pro_code,t.city_name,t.city_code,t.area_name,t.area_code,
            t.addr,t.bus_id,t.purchase_num,t.addr_url,t.pay_url,t.tbname,t.ww_name,t.order_status,t.plat_type,t.express_num,t.task_commod_id,t.pay_order_num,t.print_time,
            u.phone,u.qq,s.shop_name,s.send_name,s.send_phone");
        $query->leftJoin("buyuser bu","bu.id = t.bus_id");
        $query->leftJoin("buyuser u","u.id = t.buy_id");
        if($data['search_type'] == 8 && !empty($data['search_value']))
        {
            $query->leftJoin("taobao tb","tb.id = t.tb_id");
        }

        if($data['search_type2'] == 8 && !empty($data['search_value2']))
        {
            $query->leftJoin("taobao tb","tb.id = t.tb_id");
        }

        $query ->leftJoin("shop s","t.shop_id = s.id");
        return $query->where($where)
                      ->orderBy(" t.id desc")
                      ->offset(($data['page_num']-1)*$data['limit'])
                      ->limit($data['limit'])
                      ->asArray()
                      ->all();
    }

    private function getOrderListWhere($data)
    {
        $sql = " 1 =1 ";
        if (!empty($data['plat_type']) && isset($data['plat_type'])) {
            $sql .= " AND t.plat_type =".$data['plat_type'];
        }
        if(!empty($data['order_status']) && isset($data['order_status']))
        {
            $sql .= " AND t.order_status =".$data['order_status'];
        }
        if(!empty($data['search_type']) && isset($data['search_type']))
        {
            if($data['search_type'] == 1 && !empty($data['search_value']))
            {
                $sql .= " AND t.task_order_num = '".$data['search_value']."'";
            }else if($data['search_type'] == 2 && !empty($data['search_value']))
            {
                $sql .= " AND t.pay_order_num ='".$data['search_value']."'";
            }else if($data['search_type'] == 3 && !empty($data['search_value']))
            {
                $sql .= " AND t.express_num = '".$data['search_value']."'";
            }else if($data['search_type'] == 4 && !empty($data['search_value']))
            {
                $sql .= " AND t.ww_name ='".$data['search_value']."'";
            }else if($data['search_type'] == 5 && !empty($data['search_value']))
            {
                $sql .= " AND t.shop_name = '".$data['search_value']."'";
            }else if($data['search_type'] == 6 && !empty($data['search_value']))
            {
                $sql .= " AND bu.phone = '".$data['search_value']."'";
            }else if($data['search_type'] == 7 && !empty($data['search_value']))
            {
                $sql .= " AND u.phone = '".$data['search_value']."'";
            }else if($data['search_type'] == 8 && !empty($data['search_value']))
            {
                $sql .= " AND tb.tbname = '".$data['search_value']."'";
            }else if($data['search_type'] == 10 && !empty($data['search_value']))
            {
                $sql .= " AND t.task_id = '".$data['search_value']."'";
            }
        }
        //
        if(!empty($data['search_type2']) && isset($data['search_type2']))
        {
            if($data['search_type2'] == 1 && !empty($data['search_value2']))
            {
                $sql .= " AND t.task_order_num = '".$data['search_value2']."'";
            }else if($data['search_type2'] == 2 && !empty($data['search_value2']))
            {
                $sql .= " AND t.pay_order_num ='".$data['search_value2']."'";
            }else if($data['search_type2'] == 3 && !empty($data['search_value2']))
            {
                $sql .= " AND t.express_num = '".$data['search_value2']."'";
            }else if($data['search_type2'] == 4 && !empty($data['search_value2']))
            {
                $sql .= " AND t.ww_name ='".$data['search_value2']."'";
            }else if($data['search_type2'] == 5 && !empty($data['search_value2']))
            {
                $sql .= " AND t.shop_name = '".$data['search_value2']."'";
            }else if($data['search_type2'] == 6 && !empty($data['search_value2']))
            {
                $sql .= " AND bu.phone = '".$data['search_value2']."'";
            }else if($data['search_type2'] == 7 && !empty($data['search_value2']))
            {
                $sql .= " AND u.phone = '".$data['search_value2']."'";
            }else if($data['search_type2'] == 8 && !empty($data['search_value2']))
            {
                $sql .= " AND tb.tbname = '".$data['search_value2']."'";
            }else if($data['search_type2'] == 10 && !empty($data['search_value2']))
            {
                $sql .= " AND t.task_id = '".$data['search_value2']."'";
            }
        }

        if(!empty($data['reason']) && isset($data['reason']))
        {
            $sql .= " AND t.order_mark ='".$data['reason']."'";
        }

        if(!empty($data['begin_time']) && isset($data['begin_time']))
        {
            $sql .= " AND t.create_time>".$data['begin_time'];
        }

        if(!empty($data['end_time']) && isset($data['end_time']))
        {
            $sql .= " AND t.create_time<".$data['end_time'];
        }
        return $sql;
    }

    public function ExpessPrintList($data,$count = false) {
        $query = TaskOrder::find()
            ->alias('t')
            ->select("t.id,t.tbname,t.task_order_num,t.order_time,t.pro_name,t.city_name,t.area_name,t.addr,t.receive_name,t.receive_phone,t.order_status,t.pay_order_num,
            s.shop_name,s.pro_name as send_pro_name,s.city_name as send_city_name,s.area_name as send_area_name,s.addr as send_addr,s.dot,t.order_mark,
            b.phone")
            ->leftJoin("buyuser b","t.bus_id = b.id")
            ->leftJoin("shop s","t.bus_id = s.bus_id")
            ->where(['t.order_status' => 3])
            ->groupBy(" t.id ");//待打印
        if (!empty($data['phone'])) {
            $query = $query->andWhere(['s.send_phone' => $data['phone']]);
        }
        if ($count) {
            $count = $query->count();
            return $count;
        }
        $list = $query->offset(($data['page_num']-1)*$data['limit'])->limit($data['limit'])->asArray()->all();
        return $list;
    }

    public function getExpressPrintCout()
    {
        return TaskOrder::find()
                ->where("order_status = 3")
                ->count();
    }


    public function CountOrderList($data)
    {
        $query = TaskOrder::find()
            ->alias('t')
            ->select("t.return_type,t.task_type,t.id,task_order_num,t.create_time,buy_id,order_time,actual_price,t.pro_name,t.city_name,t.area_name,
            t.addr,t.bus_id,purchase_num,addr_url,pay_url,tbname,t.ww_name,
            phone,qq,buyuser.id as userId,shop.shop_name,send_name,send_phone")
            ->leftJoin("buyuser","t.buy_id = buyuser.id")
            ->leftJoin("shop","t.bus_id = shop.bus_id");
        if (!empty($data['plat_type'])) {
            $query = $query->andWhere(['plat_type' => $data['plat_type']]);
        }
        if (!empty($data['search_type'])) {
            switch ($data['search_type']) {
                case 1:
                    $query = $query->andWhere(['task_order_num' => $data['search_value']]);
                    break;
                case 2:
                    $query = $query->andWhere(['task_id' => $data['search_value']]);
                    break;
                case 3:
                    $query = $query->andWhere(['express_num' => $data['search_value']]);
                    break;
                case 4:
                    $query = $query->andWhere(['shop.ww_name' => $data['search_value']]);
                    break;
                case 5:
                    $query = $query->andWhere(['shop.shop_name' => $data['search_value']]);
                    break;
                case 6:
                    $query = $query->andWhere(['shop.bus_id' => $data['search_value']]);
                    break;
                case 7:
                    $query = $query->andWhere(['buyuser.id' => $data['search_value']]);
                    break;
                case 8:
                    $query = $query->andWhere(['t.tb_id' => $data['search_value']]);
                    break;
                case 9:
                    $query = $query->andWhere(['buyuser.qq' => $data['search_value']]);
                    break;
                default:
                    break;
            }
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                't.create_time',
                $data['begin_time'],
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                't.create_time',
                $data['end_time'],
            ]);
        }
        $result = $query
            ->asArray()
            ->count();
        return $result;
    }

    //统计主任务放弃次数
    public function TaskGiveUpNum($data) {
        $query = TaskOrder::find()
            ->select("task_id,count(task_id) as num")
            ->groupBy(['task_id'])
            ->where(['order_status' => 10]);
        if (!empty($data['task_id'])) {
            $query = $query->andWhere(['task_id' => $data['task_id']]);
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'give_up_time',
                $data['begin_time'],
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'give_up_time',
                $data['end_time'],
            ]);
        }
        $result = $query
            ->asArray()
            ->all();
        return $result;
    }

    //获取最后一个子任务编号
    public function GetTaskSubNo($taskId) {
        $taskSubId = TaskOrder::find()
            ->select("task_order_num")
            ->where(['task_id' => $taskId])
            ->scalar();
        return $taskSubId;
    }

	//根据主任务编号获取任务详情
	public function getTaskOrderInfo($taskId ,$select = 'id,task_id,task_commod_id,task_order_num') {
		$taskSubId = TaskOrder::find()
			->select($select)
			->where(['task_id' => $taskId])
			->asArray()
			->one();
		return $taskSubId;
	}


	//判断任务是否有人接手
    public function AcceptTaskSub($id) {
        $accept = TaskOrder::find()
            ->select("task_order_num")
            ->where(['task_id' => $id])
            ->asArray()
            ->all();
        return $accept;
    }


    public function checkUserTask($taskId,$buy_id) {
        $memberId = TaskOrder::find()
            ->select("buy_id,bus_id")
            ->where(['task_order_num' => $taskId,'buy_id' => $buy_id])
            ->asArray()
            ->one();
        return $memberId;
    }

    //获取最后放弃时间
    public function GetTaskSubTime($taskId) {
        $taskSubId = TaskOrder::find()
            ->select("give_up_time")
            ->where(['task_id' => $taskId])
            ->scalar();
        return $taskSubId;
    }

    public function TaskInfo($data) {
        $result = TaskOrder::find()
            ->alias('t')
            ->select("t.id,t.praise_keywords_id,t.commission,t.pay_url,t.keyword_url,t.pay_order_num,t.express_num,t.leave_message,
            receive_name,receive_phone,parise_url,tao_parise_url,t.task_shop_image_id,t.task_commod_id,t.order_mark,t.order_remark,t.keywords,
            t.return_type,t.task_type,t.id,task_order_num,t.create_time,
            buy_id,order_time,actual_price,t.pro_name,t.city_name,t.area_name,
            t.addr,t.bus_id,purchase_num,addr_url,pay_url,tbname,t.ww_name,
            phone,qq,buyuser.id as userId,shop.shop_name,
            task_commod.task_num,task_commod.num,task_commod.commod_link,task_commod.title,task_commod.main_url1,task_commod.price,freight_deposit")
            ->leftJoin("buyuser","t.buy_id = buyuser.id")
	        ->leftJoin('task_commod','task_commod.id = t.task_commod_id')
	        ->leftJoin("shop","task_commod.shop_id = shop.id")
            ->where(['t.id' => $data['id']])
            ->asArray()
            ->one();
	    return $result;
    }

    public function AuditDataRefus($data) {
        $query = TaskOrder::find()
            ->where(['order_status' => [1,2]])
            ->andWhere(['audit_operator' => $data['worker']]);
        if (!empty($data['begin_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        $num = $query->count();
        return $num;
    }

    public function AuditDataPass($data) {
        $query = TaskOrder::find()
            ->where(['order_status' => [3,4,5,6,7,8,9,10,11]])
            ->andWhere(['audit_operator' => $data['worker']]);
        if (!empty($data['begin_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        $num = $query->count();
        return $num;
    }

    public function checkTask($t_id) {
        $result = TaskOrder::find()
            ->select('id')
            ->where(['tb_id' => $t_id])
            ->asArray()
            ->one();
        return $result;
    }

    public function getTaskOrderByNum($Nums)
    {
        return TaskOrder::find()
                ->select("id,plat_type,task_type,task_commod_id,tb_id,tbname,task_shop_status,order_status,shop_id,pay_order_num,express_type,express_num,ww_name,receive_name,receive_phone,pro_name,
                pro_code,city_name,city_code,area_name,area_code,addr,title,buy_id,bus_id")
                ->where("task_order_num in (".$Nums.")  AND order_status = 3 AND task_shop_status = 5")
                ->asArray()
                ->all();
//       echo  $model->createCommand()->getRawSql();exit;

    }

    public function getTaskOrderCompleteCount($task_commod_id)
    {
        return TaskOrder::find()
            ->where("task_commod_id = ".$task_commod_id." AND order_status = 8")
            ->count();
    }

    //根据条件获取任务数
    public function getBuyOrderCounts($where){
	    return TaskOrder::find()->where($where)->count();
    }

    //放弃任务次数限制
	public function getBuyTaskOrderCount($buy_id, $status) {
		$sql = " buy_id = " . $buy_id . " AND order_status = " . $status;
		return TaskOrder::find()
			->where($sql)
			->count();
	}

	/**
	 * getOrderField 根据条件获取指定字段 【一维数组】
	 * @param $where
	 * @param $field
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/20
	 * @return array
	 */
	public function getOrderField($where, $field) {
		$list = TaskOrder::find()->where($where)->select('id,bus_id')->asArray()->all();
		return isset($list) ? array_keys(array_column($list, 'id', $field)) : [];
	}
	//统计待审核子订单
    public function getTaskOrderAuditCount($data)
    {
        return TaskOrder::find()
                ->where($data)
                ->count();
    }
}